/* entity */
import Tag from '@model/entity/Tag/Tag'
/* model */
import { ChartsSearchProps } from '@src/modules/report/managementReport/charts/components/common/ChartsSearch/ChartsSearch'
import { ChartsSearchParams, ChartsSearchTimePeriodType } from '@src/modules/report/managementReport/charts/components/common/ChartsSearch/ChartsSearchModel'
/* util */
import { cloneDeep } from 'lodash'
/* vue */
import { EmitEvent } from '@model/VC'
import { computed, ComputedRef } from 'vue'

const RenXiaoChartKeys: string[] = [
  'average-response-time',
  'average-service-time',
  'average-maintenance-time',
  'timely-closing-rate',
  'service-human-efficiency',
  'repeated-maintenance-rate'
]

const TaskChartKeys: string[] = [
  'task-return-visit',
  'task-customer-satisfaction'
]

const WuZiChartKeys: string[] = [
  'spare-parts-turnover',
  'spare-parts-satisfy'
]

const YingShouChartKeys: string[] = [
  'revenue-expenditure-analysis',
  // 'spare-part-sale'
]

const YingShouSparePartsChartKeys: string[] = [
  'spare-part-sale'
]

enum ChartSearchEventEnum {
  Input = 'input'
}

/** 
 * @description  图表搜索
 * @param {ChartsSearchProps} props 属性列表
 * @param {EmitEvent} emit 触发事件方法
*/
export function useChartSearch(props: ChartsSearchProps, emit: EmitEvent) {
  /**
   * @description 团队数据变化事件
   * @param {Tag[]} tags 团队数据
   */
  const handlerTeamChange = (teams: Tag[]) => {
    const tagIds: string[] = teams.map((team: Tag) => team.id)
    // const value: ChartsSearchParams = cloneDeep(props.value)

    let newValue = {
      ...props.value,
      tagIds,
      tags: teams
    }
    
    // value.tagIds = tagIds
    // value.tags = teams
    
    emit(ChartSearchEventEnum.Input, newValue)
  }

  /**
   * @description 人员数据变化事件
   * @param {string[]} userIds 人员数据
   */
   const handlerUserChange = (users: string[]) => {
    const userIds: string[] = users.map((users: string) => users)
    // const value: ChartsSearchParams = cloneDeep(props.value)
    let newValue = {
      ...props.value,
      userIds
    }
    // value.userIds = userIds
    emit(ChartSearchEventEnum.Input, newValue)
  }
  
  /** 
   * @description  工单类型数据变化事件
   * @param {string[]} types 工单类型数据
  */
  const handlerTaskTypeChange = (taskTypeIds: string[]) => {
    // const value: ChartsSearchParams = cloneDeep(props.value)
    let newValue = {
      ...props.value,
      taskTypeIds
    }
    // value.taskTypeIds = taskTypeIds
    
    emit(ChartSearchEventEnum.Input, newValue)
  }
  
  /** 
   * @description  时间周期数据变化事件
   * @param {ChartsSearchTimePeriodType} timePeriod 时间周期数据
  */
  const handlerTimePeriodChange = (timePeriod: ChartsSearchTimePeriodType) => {
    
    // const value: ChartsSearchParams = cloneDeep(props.value)
    let newValue = {
      ...props.value,
      timeStart: timePeriod.startTime,
      timeEnd: timePeriod.endTime,
      periodStartDate: timePeriod.startDate,
      periodEndDate: timePeriod.endDate,
      type: timePeriod.type
    }
    
    // value.timeStart = timePeriod.startTime
    // value.timeEnd = timePeriod.endTime
    // value.periodStartDate = timePeriod.startDate
    // value.periodEndDate = timePeriod.endDate
    // value.type = timePeriod.type
    
    emit(ChartSearchEventEnum.Input, newValue)
  }

  /** 
   * @description  仓库数据变化事件
   * @param {string} repertoryIdList 仓库数据
  */
  const handlerRepertoryChange = (repertoryIdList: string) => {
    // const value: ChartsSearchParams = cloneDeep(props.value)

    let newValue = {
      ...props.value,
      repertoryIdList
    }

    emit(ChartSearchEventEnum.Input, newValue)
  }

  /** 
   * @description  备件品类数据变化事件
   * @param {string[]} sparepartIdList 备件品类数据
  */
  const handlerSparePartsChange = (sparepartIdList: string[]) => {
    let newValue = {
      ...props.value,
      sparepartIdList
    }

    emit(ChartSearchEventEnum.Input, newValue)
  }

  /** 
   * @description  服务类型数据变化事件
   * @param {string[]} serviceTypeList 服务类型数据
  */
  const handlerServiceTypeChange = (serviceTypeList: string[]) => {
    let newValue = {
      ...props.value,
      serviceTypeList
    }

    emit(ChartSearchEventEnum.Input, newValue)
  }
  
  return {
    handlerTeamChange,
    handlerUserChange,
    handlerTaskTypeChange,
    handlerTimePeriodChange,
    handlerRepertoryChange,
    handlerSparePartsChange,
    handlerServiceTypeChange
  }
}

export function useChartSearchShow(props: ChartsSearchProps) {
  
  // 是否为人效图表搜索
  const isRenXiaoChartSearch: ComputedRef<boolean> = computed(() => {
    return RenXiaoChartKeys.includes(props.reportKey)
  })
  
  // 是否为工单图表搜索
  const isTaskChartSearch: ComputedRef<boolean> = computed(() => {
    return TaskChartKeys.includes(props.reportKey)
  })
  
  // 是否为物资图表搜索
  const isWuZiChartSearch: ComputedRef<boolean> = computed(() => {
    return WuZiChartKeys.includes(props.reportKey)
  })
  
  // 是否为营收图表搜索
  const isYingShouChartSearch: ComputedRef<boolean> = computed(() => {
    return YingShouChartKeys.includes(props.reportKey)
  })

  // 是否是备件营收分析搜索
  const isYingShouSparePartsChartSearch: ComputedRef<boolean> = computed(() => {
    return YingShouSparePartsChartKeys.includes(props.reportKey)
  })
  
  return {
    isRenXiaoChartSearch,
    isTaskChartSearch,
    isWuZiChartSearch,
    isYingShouChartSearch,
    isYingShouSparePartsChartSearch
  }
}